import time
from PIL import Image
from camera.frame_camera import AVStreamDecoder
from workers.detect_worker import frame_ququqe
from tools.log import logger
# 存储帧格式信息
frame_info = {"width": None, "height": None, "format": None}
cnt = 0
st = time.time()
def parse_caps(caps):
    caps_struct = caps.get_structure(0)
    frame_info["width"] = caps_struct.get_value("width")
    frame_info["height"] = caps_struct.get_value("height")
    frame_info["format"] = caps_struct.get_value("format")  # 如 "RGB"
    return frame_info

def work(global_conf:dict,camera_conf:dict):
    cam = AVStreamDecoder(conf=camera_conf["frame_camera"],on_frame=lambda frame:on_new_sample(frame,camera_conf["id"]))
    cam.run()

def print_time(st):
    print(f"{(time.time()-st)*1000}ms")
            
def on_new_sample(frame,id:str):
    # 处理帧的回调函数
    ts = time.time()
    global cnt
    frame_ququqe.put((frame,ts))
    cnt = cnt+1
    if cnt%60==0:
        logger.info(f"camera-{id}  {cnt/(st-time.time()):.1f} fps")
